const path = require('path');
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ip = require('ip').address();
const webpackConfig = {
  mode: "development",
  entry: {
    index: './src/web/index.js',
    mobile: './src/mobile/index.js',
  },
  output: {
    path: path.resolve(__dirname, './admin'),
    publicPath: '/',
    chunkFilename: '[name].js',
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      'vue': 'vue/dist/vue',
      '@': path.resolve(__dirname, './src'),
      'component': path.resolve(__dirname, './src/components/web'),
      'mobile': path.resolve(__dirname, './src/components/mobile'),
    }
  },
  devServer: {
    // host: ip,
    host: 'localhost',
    // historyApiFallback: true,
    inline: true,
    port: 8084,
    publicPath: '/',
    proxy: {
      '/api': {
        // target: 'http://121.40.240.199:8001/api', // 运营接口的域名
        // target: 'http://delaicloud.com:28011/bee', // 商户接口的域名
        target: 'http://delaicloud.com:28013/locker',
        // target: 'http://192.168.1.35:9091/ms', // 本地接口的域名
        secure: false,
        changeOrigin: false,
        pathRewrite: { '^/api': '' },
      }
    },
  },
  //提取公共代码缩小入口文件体积
  optimization: {
    splitChunks: {
      name: "commons",
      chunks: "all",
      maxAsyncRequests: 2
    },
    runtimeChunk: {
      name: 'runtime'
    }
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      title: '能源管理系统',
      inject: true,
      chunks: ['index', 'runtime', 'commons'],
      hash: true,
      favicon: './favicon.ico',
    }),
    new HtmlWebpackPlugin({
      filename: 'mobileIndex.html',
      template: 'mobileIndex.html',
      title: '蜜蜂钱包',
      inject: true,
      chunks: ['mobile', 'runtime', 'commons'],
      hash: true,
    }),
  ],
  module: {
    rules: [
      { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', } },
      { test: /\.json$/, use: 'json-loader' },
      { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' },
      { test: /\.vue$/, use: 'vue-loader' },
      {
        test: /\.css$/, use: [{ loader: 'style-loader' }, { loader: "css-loader" },
        {
          loader: "postcss-loader",
          options: {
            plugins: [
              require('autoprefixer')("last 100 versions")
            ]
          }
        }
        ]
      },
      { test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/, loader: 'file-loader?name=font/[name]-[hash].[ext]' },
    ]
  }
};
module.exports = webpackConfig
